﻿using System;
using System.Resources;
using System.Reflection;
using System.Text;
using System.Diagnostics;


namespace MultiConvert
{
    class MultiConvertMain
    {        
        static private MultiConvertParameters Parameters;
        static private MultiConvertApplicationWrapper Framework;        
        static private String LineMessage;   
        
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
#if TRACE
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));            
            Trace.AutoFlush = true;
            Trace.Indent();
            Console.ForegroundColor = ConsoleColor.Cyan;
#endif
            Parameters = new MultiConvertParameters(args);
            if (Parameters.GetHelp == true || Parameters.ConvertMethod == null)
            {
                Console.WriteLine(new HelpContext());                
            }
            try
            {
                switch (Parameters.FromExtension.ToLowerInvariant())
                {
                    case ".asm":
                    case ".par":
                	case ".psm":                		
                	case ".pwd":
                    case ".dft":
                        Framework = MultiConvert.MultiConvertApplicationWrapper.ApplicationFactory("SolidEdge.Application");
                        break;                    
                        
                    case ".doc":
                        Framework = MultiConvert.MultiConvertApplicationWrapper.ApplicationFactory("Word.Application");
                        break;
                    default:
                        throw new ConvertException(String.Format(Properties.Resources.ExNoApplication, Parameters.FromExtension));
                }
                
                Framework.ArrangeApplication();
                Framework.OpenDocument(Parameters.FromFile);
                
                // ToFile zuvor löschen, wenn vorhanden.
                try { System.IO.File.Delete(Parameters.ToFile);	}	            
	            catch { }

                // Convertierung durchführen, falls Document vorhanden.
                if (Framework.Document != null) LineMessage = Framework.ConvertDocument(Parameters.ConvertMethod, Parameters.ToFile);                                
                Framework.CloseDocuments();
                Framework.ReArrangeApplication();
                Console.WriteLine((DashLine)55 + "--- MulitConvert ---");
                Console.WriteLine(DateTime.Now);
                Console.WriteLine(Parameters.ConvertMethod + " " + LineMessage);
                Console.WriteLine(Parameters.FromFile + " to " + Parameters.ToFile);                
                Console.WriteLine((DashLine)75);

            }
            catch (Exception ex)
	        {
                Console.WriteLine((DashLine)55 + "--- MulitConvert ---");                
                Console.WriteLine(DateTime.Now);               
                Console.WriteLine(ex.Message.ToString());
                Console.WriteLine((DashLine)75);
	        }
#if DEBUG
            Console.ReadKey();
#endif 
        }        
    }
}